package com.amat0urheu.controller.adminController;


import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.pojo.CloudPojo.Scene;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.SceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class SceneController {

    @Autowired
    SceneService sceneService;
    //    @PreAuthorize("hasAuthority('ROLE_user')")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/cloud/getAllScene/{pageSize}/{pageNum}")
    ReturnRes getIpPort(@PathVariable int pageSize,
                              @PathVariable int pageNum) {

        return   sceneService.getScenes(pageSize, pageNum);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/cloud/getExperimentScene")
    ReturnRes getExperimentScene() {

        return    sceneService.getExperimentScenes();
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/saveScene")
    public ReturnRes saveScene(@RequestParam(name = "data") String sceneJson) {
        Scene scene = JSONObject.parseObject(sceneJson, Scene.class);
        if(scene.getScene().equals("")||scene.getLinkway()==null||scene.getClientId()==null){
            return  new ReturnRes(false, "请填写必要参数");
        }
        return  sceneService.saveScene(scene);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/scene/search/{pageSize}/{pageNum}")
    public ReturnRes searchScenePage(@PathVariable int pageSize,
                                              @PathVariable int pageNum,
                                              @RequestParam String key,
                                              @RequestParam String value) {
        return sceneService.searchScenePage(key, value, pageNum, pageSize);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/udateScene")
    public ReturnRes udateScene(@RequestParam(name = "data") String sceneJson) {
        Scene scene = JSONObject.parseObject(sceneJson, Scene.class);

        if(scene.getScene().equals("")||scene.getLinkway()==null||scene.getClientId()==null||scene.getVictimIds2().size()==0){
            return  new ReturnRes(false, "请填写必要参数");
        }


        return  sceneService.updateScene(scene);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/removeScene")
    ReturnRes removeScene(@RequestParam(name = "data") Integer sId) {

        return   sceneService.removeScene(sId);
    }

}